package com.wwj.auction.login.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.auction.login.entity.UserInfo;
import com.wwj.auction.login.entity.Withdraw;
import com.wwj.auction.login.enums.AuditStatus;
import com.wwj.auction.login.mapper.UserInfoMapper;
import com.wwj.auction.login.mapper.WithdrawMapper;
import com.wwj.auction.login.service.TransFlowService;
import com.wwj.auction.login.service.UserAccountService;
import com.wwj.auction.login.service.WithdrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wwj
 * @since 2021-08-10
 */
@Service
public class WithdrawServiceImpl extends ServiceImpl<WithdrawMapper, Withdraw> implements WithdrawService {

    @Resource
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserAccountService userAccountService;
    @Autowired
    private TransFlowService transFlowService;

    @Override
    public void save(Integer userId, String fetchAmt) {
        // 查询当前用户信息
        UserInfo userInfo = userInfoMapper.selectById(userId);
        // 封装提现信息
        Withdraw withdraw = new Withdraw();
        withdraw.setUserId(userId);
        // 提现金额
        BigDecimal fetchAmtDecimal = new BigDecimal(fetchAmt);
        withdraw.setWithdrawAmount(fetchAmtDecimal);
        // 手续费，收取提现金额的0.1%
        BigDecimal poundage = fetchAmtDecimal.multiply(new BigDecimal("0.001")).setScale(2, BigDecimal.ROUND_HALF_UP);
        withdraw.setPoundage(poundage);
        // 申请状态设置为审核中
        withdraw.setStatus(AuditStatus.AUDIT_ING.getStatus());
        // 用户姓名
        withdraw.setUserName(userInfo.getName());
        // 手机号
        withdraw.setPhone(userInfo.getPhone());
        // 性别
        withdraw.setSex(userInfo.getSex());
        // 审核时间
        withdraw.setCreateTime(LocalDateTime.now());
        // 身份证号
        withdraw.setIdCard(userInfo.getIdCard());
        // 保存
        baseMapper.insert(withdraw);

        // 当审核完成后款项才会提现成功，需要暂时将提现金额冻结起来
        userAccountService.freezeMoney(userId, fetchAmt);
        // 记录交易流水
        transFlowService.saveFlow(userId, fetchAmt);
    }

    @Override
    public Integer getStatus(Integer userId) {
        // 通过id查询提现状态，需要查询是否存在正在审核的提现流程
        Withdraw withdraw = baseMapper.selectOne(
                Wrappers.lambdaQuery(Withdraw.class).
                        eq(Withdraw::getUserId, userId)
                        .eq(Withdraw::getStatus, AuditStatus.AUDIT_ING.getStatus()));
        if (withdraw == null) {
            // 若为空，则说明当前用户没有正在提现的款项
            // 查询提现表中的最新一条数据
            withdraw = baseMapper.getNewestData();
            // 若仍然为空，则说明当前用户没有任何提现内容
            if(withdraw == null){
                return 0;
            }else {
                // 否则返回最新数据的状态
                return withdraw.getStatus();
            }
        }
        return withdraw.getStatus();
    }
}
